package com.xymtop.sqlcater.core.sql.sqlexecute.select.entity;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

//描述一个查询对象
@Data
public class SqlSelect {

    private String name;

            /*
            * SELECT
            bx_inspec_node_config.*,
            sys_user.*
        FROM
            bx_inspec_node_config,
            sys_user
        WHERE
            bx_inspec_node_config.create_man = sys_user.id
        GROUP BY
            bx_inspec_node_config.id,
            sys_user.realname
        HAVING
            bx_inspec_node_config.id = bx_inspec_node_config.remark
        ORDER BY
            bx_inspec_node_config.`name` ASC
    *
    *
    * */

    //需要查询的语句
    private String sql;

    //需要查询的列
    private List<SqlSelectField> sqlSelectFields = new ArrayList<>();

    //从哪些表里面查询
    private List<SelectCollect> selectCollects = new ArrayList<>();


    //    连接对象
    private List<SqlJoin> sqlJoins = new ArrayList<>();


    //条件对象
    private SqlWhere sqlWhere;


    //分组
    private List<Group> groups = new ArrayList<>();

    //Having

    private List<Having> havings = new ArrayList<>();

    //ORDERBY

    private List<OrderBy> orderByList = new ArrayList<>();


    //会有很多的子语句
    private  List<SqlSelect> subSelects = new ArrayList<>();


    //其它的SQL，如limit
    private String other;

    public String buildJson(){

        return name + ":"+ sql ;
    }

    //构建SQL语句
   public String buildSql(){
       String sqlStr = "";

       //查询
       sqlStr+="SELECT ";

       //查询的列
       if(sqlSelectFields!=null&&!sqlSelectFields.isEmpty()){
           for(SqlSelectField sqlSelectField :sqlSelectFields){

               if(sqlSelectField!=null){
                   String asName = sqlSelectField.getAsName();
                   sqlStr+=asName==null?sqlSelectField.getName():sqlSelectField.getAsName()+",";
               }
           }

//           sqlStr=sqlStr.substring(0,sqlStr.length()-1);

           sqlStr+=" FROM ";
           //查询的集合
           if(selectCollects!=null){
               for(SelectCollect selectCollect:selectCollects){

                   if(selectCollect.getSelectCollectType()==SelectCollectType.SQLQUERY){
                       sqlStr+="@"+selectCollect.getName()+" ,";
                   }else {
                       sqlStr+=selectCollect.getName()+",";
                   }

               }
               sqlStr=sqlStr.substring(0,sqlStr.length()-1);
           }

       }


       //连接查询
       if(sqlJoins!=null){

           for (SqlJoin sqlJoin : sqlJoins){

            sqlStr +=  sqlJoin.buildSql();

           }
       }

       //分组查询

       if(groups!=null){
           for(Group group :groups){
              sqlStr += group.getBuildSql();
           }
       }

       //Having查询
       if(havings!=null){
           for(Having having : havings){
               sqlStr +=  having.getBuildSql();
           }
       }

       //排序
       if(orderByList!=null){
           for (OrderBy orderBy : orderByList){
               sqlStr +=  orderBy.getBuildSql();
           }
       }


       other = "LIMIT 100";
       //添加其它的
       if(other!=null){
           sqlStr += other;
       }

       if(this.sql==null){
           this.sql = sqlStr;
       }

       if(name==null){
           name = "run";
       }

       return sqlStr;
   }

}
